<template>
  <section>
    <!--工具条-->
    <!-- <div>{{winWidth}}</div>
    <div>{{winHeight}}</div> -->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-row :gutter="40">
          <el-col :xs="24" :sm="12" :md="6" :lg="6" style="margin-bottom:5px;">
            <el-input size="small" style="width:100%;" v-model.trim="filters.userCode" placeholder="用户编码"></el-input>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" :lg="6" style="margin-bottom:5px;">
            <el-input size="small" style="width:100%;" v-model.trim="filters.mark" placeholder="标识"></el-input>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" :lg="6" style="margin-bottom:5px;">
            <el-input size="small" style="width:100%;" v-model.trim="filters.operation" placeholder="操作"></el-input>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" :lg="6" style="margin-bottom:5px;">
            <el-input size="small" style="width:100%;" v-model.trim="filters.requestData" placeholder="请求数据"></el-input>
          </el-col>
        </el-row>

        <el-row :gutter="40">
          <el-col :xs="24" :sm="12" :md="6" :lg="6" style="margin-bottom:5px;">
            <el-input size="small" style="width:100%;" v-model.trim="filters.responseData" placeholder="响应数据"></el-input>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="6" style="margin-bottom:5px;">
            <el-date-picker size="small" style="width:100%;" v-model="filters.requestTime" type="datetimerange" align="left" placeholder="选择请求时间范围">
            </el-date-picker>
          </el-col>
          <el-form-item style="float:right;margin-right:20px;">
            <el-button size="small" v-show="exportBtnVisible" type="primary" icon="upload2" @click="exportExcels">导出</el-button>
          </el-form-item>
          <el-form-item style="float:right;">
            <el-button size="small" v-show="queryBtnVisible" type="primary" icon="search" v-on:click="getLogs(true)">查询</el-button>
          </el-form-item>

        </el-row>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="Logs" stripe border :height="winHeight-239" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" @cell-click="dblclick" :cell-style="{height:'10px',color:'#ccc'}">
      <!-- <el-table-column type="selection" width="55">
      </el-table-column> -->
      <el-table-column prop="id" label="ID" width="80" align="center">
        <template scope="scope">
          <span style="color:#20a0ff;cursor:pointer;text-decoration:underline;">{{scope.row.id}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="userCode" label="用户编码" width="150" show-overflow-tooltip>
        <!-- <el-tooltip class="item" effect="dark" :content="scope.row.shareId" placement="top"> -->
        <!-- <template scope="scope">
            <span style="height:20px;">{{scope.row.shareId}}</span>
          </template> -->
        <!-- </el-tooltip> -->
      </el-table-column>
      <el-table-column prop="mark" label="标识" width="150">
      </el-table-column>
      <el-table-column prop="operation" label="操作" width="100" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="requestData" label="请求数据" width="200" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="responseData" label="响应数据" width="200" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="requestTime" label="请求时间" min-width="150" sortable>
      </el-table-column>
      <el-table-column label="操作" fixed='right' width="100">
        <template scope="scope">
          <!--  <el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button> -->
          <el-button type="danger" v-show="deleteBtnVisible" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->

      <el-pagination style="float:right" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-sizes="[10, 20, 40, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-col>

    <!--详情界面-->
    <el-dialog title="详情" v-model="detailFormVisible" :close-on-click-modal="false">
      <el-form :model="detailForm" label-width="100px" ref="detailForm">
        <div>
          <span>ID:</span>
          <span>{{detailForm.id}}</span>
        </div>
        <div style="margin-top:10px;">
          <span>用户编码:</span>
          <span>{{detailForm.userCode}}</span>
        </div>
        <div style="margin-top:10px;">
          <span>请求数据:</span>
          <div style="word-wrap:break-word" v-html="detailForm.requestData"></div>
        </div>
        <div style="margin-top:10px;">
          <span>响应数据:</span>
          <div style="word-wrap:break-word" v-html="detailForm.responseData"></div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="detailFormVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'

import commons from '../../common/js/common'
//import NProgress from 'nprogress'
import { mapState, mapActions } from 'vuex'
import { querySaleLog, deleteSaleLog } from '../../api/api';

export default {
  data() {
    return {
      filters: {
        userCode: '',
        mark: '',
        operation: '',
        requestData: '',
        responseData: '',
        requestTime: ''
      },
      Logs: [],
      lists: [],
      exportTotal: 0,
      total: 0,
      page: 1,
      pageSize: 10,
      loading: '',
      // fullscreenLoading:false,
      listLoading: false,
      detailFormVisible: false,
      detailForm: {

      },
      sels: [],//列表选中列
      userid: '',
      IsShow: false,

      queryBtnVisible: false,
      exportBtnVisible: false,
      deleteBtnVisible: false
    }
  },
  computed: mapState([
    'winWidth',
    'winHeight'
  ]),
  methods: {
    cellStyle(row, rowIndex, columnIndex) {
    },
    formatStatus: function (row, column) {
      return row.level == '1' ? '一级' : (row.level == '2' ? '二级' : '三级');
    },
    formatTime: function (row, column) {
      return commons.getDaytime(row.requestTime, 1, false);
    },
    dblclick(row, column, cell, event) {
      if (column.label == "ID") {
        this.detailFormVisible = true;
        this.detailForm = row;
      }
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getLogs(true);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getLogs(true);
    },
    //获取用户列表
    getLogs(bool) {
      let para = new FormData();
      para.append("pageSize", bool ? this.pageSize : this.exportTotal);
      para.append("pageNumber", bool ? this.page : 1);
      para.append("userCode", this.filters.userCode);
      para.append("mark", this.filters.mark);
      para.append("operation", this.filters.operation);
      para.append("requestData", this.filters.requestData);
      para.append("responseData", this.filters.responseData);
      para.append("startTime", commons.changeTimes(this.filters.requestTime, 'start', true));
      para.append("endTime", commons.changeTimes(this.filters.requestTime, 'end', true));
      this.listLoading = bool;
      // this.fullscreenLoading = bool?false:true;
      if (!bool) {
        this.loading = this.$loading({
          lock: true,
          text: '正在导出',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      }
      querySaleLog(para).then((res) => {
        this.lists = res.data.result.content;
        this.total = res.data.result.totalElements;
        this.exportTotal = res.data.result.totalElements;
        this.listLoading = false;
        if (!bool) {
          this.loading.close();
          this.export2Excel();
        }
        else {
          this.Logs = res.data.result.content;
        }
      });
    },
    //table导出excel
    exportExcels: function () {
      this.getLogs(false);
    },
    export2Excel: function () {
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../vendor/Export2Excel');
        const tHeader = ['ID', '用户编码', '标识', '操作', '请求数据', '响应数据', '请求时间'];
        const filterVal = ['id', 'userCode', 'mark', 'operation', 'requestData', 'responseData', 'requestTime'];
        const list = this.lists;
        // for (var i in list) {
        //   list[i].requestTime = commons.getDaytime(list[i].requestTime, 1, false);
        // }
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, '销售日志数据excel');
      })
    },
    formatJson: function (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },


    selsChange: function (sels) {
      this.sels = sels;
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let para = new FormData();
        para.append("id", row.id);
        deleteSaleLog(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getLogs(true);
        });
      }).catch(() => {
        this.listLoading = false;
      });
    },

    btnShow: function () {
      let thisData = JSON.parse(sessionStorage.getItem('147'));
      let _this = this;
      $.each(thisData, function (i, j) {
        if (thisData[i] == "salelogQuery") {
          _this.queryBtnVisible = true;
        }
        if (thisData[i] == "salelogExport") {
          _this.exportBtnVisible = true;
        }
        if (thisData[i] == "salelogRemove") {
          _this.deleteBtnVisible = true;
        }
      });
    }
  },
  mounted() {
    this.getLogs(true);
    this.btnShow();
  }
}

</script>

<style scoped>

</style>